import re
from playwright.sync_api import sync_playwright, Page

from _hellchin_webrun.src.core.browser.base import BrowserMixin
from _hellchin_webrun.src.core.browser.elements.element import ElementMixin
from _hellchin_webrun.src.core.browser.pages.navigation import PageNavMixin
from _hellchin_webrun.src.core.browser.pages.util import PageUtilsMixin
from _hellchin_webrun.src.core.browser.pages.waiting import PageWaitMixin
from _hellchin_webrun.src.core.engine.assertions import AssertMixin
from _hellchin_webrun.src.core.engine.models import ConfigModel
# from _hellchin_webrun.src.core.utils.chinese_alias import ChineseAliasMeta
# from _hellchin_webrun.src.core.browser.base import BaseTest


class ActionStep(BrowserMixin, PageNavMixin, ElementMixin, PageWaitMixin, PageUtilsMixin, AssertMixin):
    """
    执行步骤类，封装了页面对象，元素对象，鼠标键盘对象，断言对象等等操作
    """

    def __init__(
            self,
            config: ConfigModel,
            browser=None,
            context=None,
            page: Page = None,
            log_obj=None
    ) -> None:
        super().__init__(config, browser, context, page, log_obj)

    def perform(self, step):
        """执行测试步骤的方法"""

        self.log.info(f'执行步骤: {step.desc}')

        # 获取测试步骤的方法名称
        method = step.method or step.keyword

        # 判断方法是否存在
        if hasattr(self, method):
            # 获取测试步骤的参数
            params = step.params  # get("params", {})
            # 替换参数
            params = self.replace_params(params)
            # 执行方法
            getattr(self, method)(**params)
        else:
            # 抛出异常
            raise AttributeError(f"执行的步骤: {step.desc}，执行方法 【{method}】 不存在")
